package com.aspirecn.auth.config;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;

import java.util.Properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class KaptchaConfig {

    @Value("${kaptcha.border}") 
    private String border;
	
    @Value("${kaptcha.session.key}")  
    private String skey;  
      
    @Value("${kaptcha.textproducer.font.color}")  
    private String fcolor;  
      
    @Value("${kaptcha.textproducer.font.size}")  
    private String fsize;  
      
    @Value("${kaptcha.obscurificator.impl}")  
    private String obscurificator;  
      
    @Value("${kaptcha.noise.impl}")  
    private String noise;  
      
    @Value("${kaptcha.image.width}")  
    private String width;  
      
    @Value("${kaptcha.image.height}")  
    private String height;  
      
    @Value("${kaptcha.textproducer.char.length}")  
    private String clength;  
      
    @Value("${kaptcha.textproducer.char.space}")  
    private String cspace;  
      
    @Value("${kaptcha.background.clear.from}")  
    private String from;  
      
    @Value("${kaptcha.background.clear.to}")  
    private String to;

    @Value("${kaptcha.textproducer.font.names}")  
	private String fnames; 
	
	/* 声明验证码生成策略属性 Bean */
	@Bean
	public DefaultKaptcha captchaProducer() {
		DefaultKaptcha captchaProducer = new DefaultKaptcha();
		Properties properties = new Properties();
		properties.setProperty("kaptcha.border", border);
		//properties.setProperty("kaptcha.border.color", "105,179,90");
		properties.setProperty("kaptcha.textproducer.font.color", fcolor);
		properties.setProperty("kaptcha.textproducer.font.size", fsize);
		properties.setProperty("kaptcha.obscurificator.impl", obscurificator);
		properties.setProperty("kaptcha.noise.impl", noise);
		properties.setProperty("kaptcha.image.width", width);
		properties.setProperty("kaptcha.image.height", height);
		properties.setProperty("kaptcha.session.key", skey);
		properties.setProperty("kaptcha.textproducer.char.length", clength);
		properties.setProperty("kaptcha.textproducer.char.space", cspace);
		properties.setProperty("kaptcha.background.clear.from", from);
		properties.setProperty("kaptcha.background.clear.to", to);
		properties.setProperty("kaptcha.textproducer.font.names", fnames);
		Config config = new Config(properties);
		captchaProducer.setConfig(config);
		return captchaProducer;
	}
	
}
